<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-23 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';
require_once ROOT_PATH.'include/lib/image.func.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		$this->excute();
	}
	
	private function excute()
	{
		switch($this->act)
		{
			case 'add':
				$this->addComplaint();
				break;
			case 'do_favorite':
				//$this->doFavorite();
				break;
			default:
				$this->message('g_04', STATUS_E);
				break;
		}
		
		$this->showResponse();
	}
	
	private function addComplaint()
	{
		//DBFunction::getInstance()->setDebug();
		
		$this->request['bid'] = isset($this->request['bid']) ? (int)$this->request['bid'] : '';
		$this->request['uid'] = isset($this->request['uid']) ? (int)$this->request['uid'] : '';
		$this->request['reason'] = isset($this->request['reason']) ? $this->request['reason'] : '';
		$this->request['address'] = isset($this->request['address']) ? $this->request['address'] : '';
		$this->request['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		$this->request['telphone'] = isset($this->request['telphone']) ? $this->request['telphone'] : '';
		
		$business = get_travel_item(array('id'=>$this->request['bid']));
		
		if(!check_array($business) || !isset($business['id']) || $business['id'] < 1)
		{
			$this->message('b_01', STATUS_E);
		}
		
		$image_str = isset($this->request['image']) ? $this->request['image'] : '';
		$image_type = isset($this->request['image_type']) ? $this->request['image_type'] : '';
		
		$this->request['image_path'] = '';
		if($image_str)
		{
			$tmp_img = save_image_file($image_str, $image_type);
			
			if(isset($tmp_img['name']) && $tmp_img['name'])
			{
				
				$_FILES['image'] = $tmp_img;
				
				$image_dir = 'data/images/b_complaint/'.rand(1, 99).'/';
				$image_path = move_image_to_path('image', $image_dir);
				if($image_path)
				{
					$this->request['image_path'] = $image_path;
				}
			}
		}
		
		$result = add_complaint($this->request);
		
		if($result)
		{
			$this->response['status'] = STATUS_S;
			$this->response['message_code'] = 'cp_01';
		}
		else
		{
			$this->response['status'] = STATUS_F;
			$this->response['message_code'] = 'cp_02';
		}
	}
	
	private function doFavorite()
	{
		$cid = isset($this->request['cid']) ? $this->request['cid'] : '';
		
		$result = get_complaint(array('cid'=>$cid));
		
		if(!check_array($result)) $this->message('cp_03', STATUS_F);
		
		$result = update_complaint_favorite_count(array('id'=>$cid));
		
		if($result)
		{
			$this->response['status'] = STATUS_S;
			$this->response['message_code'] = 'g_06';
		}
		else
		{
			$this->response['status'] = STATUS_F;
			$this->response['message_code'] = 'g_07';
		}
	}
}